package com.weitu.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by WT-SUN on 2017/6/6.
 */
public class TableTreeNodeUtil {

    /**
     * 获取父节点
     *
     * @param treeDataList
     * @return
     */
    public final static List<Map<String,Object>> getFatherNode(List<Map<String,Object>> treeDataList) {
        List<Map<String,Object>> newTreeDataList = new ArrayList<Map<String,Object>>();
        List<Object> pIdList = new ArrayList<Object>();
        List<Object> idList = new ArrayList<Object>();
        for (Map<String,Object> jsonTreeData:treeDataList){
            pIdList.add(jsonTreeData.get("parentId").toString());
            idList.add(jsonTreeData.get("id").toString());
        }
        String pId = null;
        for (int i = pIdList.size(); i > 0; i--) {
            if (idList.contains(pIdList.get(i - 1))) {
                pIdList.remove(i - 1);
            }
        }
        if (pIdList.size() > 0) {
            pId = pIdList.get(0) == null ? null : pIdList.get(0).toString();
        }
        if (pId != null) {
            for (Map<String,Object> jsonTreeData : treeDataList) {
                if (pId.equals(jsonTreeData.get("parentId"))) {
                    jsonTreeData.put("children",getChildrenNode(jsonTreeData.get("id").toString(), treeDataList));
                    if (jsonTreeData.get("children") instanceof List) {
                        if (((List) jsonTreeData.get("children")).size()>0)
                            jsonTreeData.put("state","open");
                    }
                    newTreeDataList.add(jsonTreeData);
                }
            }
        } else {
            for (Map<String,Object> jsonTreeData : treeDataList) {
                if (jsonTreeData.get("parentId") == null) {
                    jsonTreeData.put("children",getChildrenNode(jsonTreeData.get("id").toString(), treeDataList));
                    if (jsonTreeData.get("children") instanceof List) {
                        if (((List) jsonTreeData.get("children")).size()>0)
                            jsonTreeData.put("state","open");
                    }
                    newTreeDataList.add(jsonTreeData);
                }
            }
        }

        return newTreeDataList;
    }

    /**
     * @param pid
     * @param treeDataList
     * @return
     */
    private final static List<Map<String,Object>> getChildrenNode(String pid, List<Map<String,Object>> treeDataList) {
        List<Map<String,Object>> newTreeDataList = new ArrayList<Map<String,Object>>();
        for (Map<String,Object> jsonTreeData : treeDataList) {
            if (jsonTreeData.get("parentId") == null) continue;
            //这是一个子节点
            if (jsonTreeData.get("parentId").equals(pid)) {
                //递归获取子节点下的子节点
                jsonTreeData.put("children",getChildrenNode(jsonTreeData.get("id").toString(), treeDataList));
                if (jsonTreeData.get("children") instanceof List) {
                    if (((List) jsonTreeData.get("children")).size()>0)
                    jsonTreeData.put("state","closed");
                }
                newTreeDataList.add(jsonTreeData);
            }
        }
        return newTreeDataList;
    }
}
